app.controller('GetOneBuildingCtrl', ['$scope','$http','$location','$localStorage','$window','$state',
 function($scope,$http,$location,$localStorage,$window,$state) {
 		$scope.oo = {};
 	    $scope.namedata = $localStorage.cur_user.uname;
 		$http.post(
	        $scope.app.backendurl+"tbl-campus-building/view-one",
	        {  
	        	'school_id':$state.params.cmid,	        	
	            'campus_building_id': $state.params.cbid,       
	            'token':$localStorage.token                  

	        }
	    ).success(function (data) {
	    	//console.log(data);
	    	if(data.code==0&&data.msg==""){
	    		$scope.oo = data.data;	    		
	    		$scope.service_man = data.data.user_id + ',' + data.data.phone;
	    		$scope.aaData = data.data;
	    	}
	    	
	    }).error(function (data) {
	    	alert(data);
	    })

	    $scope.save = function(aa){	    	
	    	var layersJson = [];
	    	var serviceman_id;
	    	for(var i=0;i<aa.length;i++){
	    		layersJson.push(aa[i].layer_name);
	    	}	    	
	    	if($scope.service_man!=null&&$scope.service_man!=""&&$scope.service_man!=undefined){
	    		serviceman_id = $scope.service_man.split(',')[0];
	    	}else{
	    		serviceman_id = ''
	    	}
	    	$http.post(
		        $scope.app.backendurl+"tbl-campus-building/update",
		        {
		            'school_id':$state.params.cmid,
		            'campus_building_id':$state.params.cbid,
		            'building_name': $scope.oo.building_name,
		            'layer_amount': $scope.oo.layer_amount,
		            'serviceman_id':serviceman_id,
		            'layersJson':JSON.stringify(layersJson),    
		            'token':$localStorage.token                

		        }
		    ).success(function (data) {
		      //console.log(data);
		      if(data.code==0&&data.msg=="楼修改成功"){
		        alert(data.msg);
		        $window.location.reload('app/sys/campusconfigedit');  
		      }
		      if(data.code==1){
		      	alert(data.msg+"，请重新输入");
		      	$window.location.reload('app/sys/campusconfigedit');  
		      }
		       if(data.code==20001){
		         $location.path('access.signin');
		       }
		    }).error(function (data) {
		        $scope.authError = data.error.code;
		        if ($scope.authError == 401) {
		            $location.path('access.signin');
		        }
		    })
	    }
  

 }])